package com.njworkorder.Controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njworkorder.DTO.DictionaryTypeDTO;
import com.njworkorder.Entity.DictionaryType;
import com.njworkorder.Service.DictionaryTypeService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.UUID;

@Tag(name = "字典类型相关接口", description = "字典类型相关接口")
@RestController
@RequestMapping("/Dic")
public class DictionaryTypeController {

    private final DictionaryTypeService dictionaryTypeService;
    public DictionaryTypeController(DictionaryTypeService dictionaryTypeService) {
        this.dictionaryTypeService = dictionaryTypeService;
    }


    @Operation(summary = "新增系统字典类型列表")
    @PostMapping("/DfInfo")
    public Result<String> addInfo(@RequestBody DictionaryTypeDTO dictionaryTypeDTO,
                                  @RequestHeader("Create-Ap-Id") String createApId){
        LambdaQueryWrapper<DictionaryType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DictionaryType::getType,dictionaryTypeDTO.getType());
        long count = dictionaryTypeService.count(lambdaQueryWrapper);
        if (count>0){
            return ResultUtil.fail("名称已存在!");
        }
        DictionaryType dit = new DictionaryType();
        BeanUtils.copyProperties(dictionaryTypeDTO,dit);
        String id = UUID.randomUUID().toString();
        dit.setId(id);
        dit.setCreateApId(createApId);

        boolean save = dictionaryTypeService.save(dit);
        if (save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }
    @Operation(summary = "获取系统字典类型配置列表")
    @GetMapping("/getDictList")
    public Result<Page<DictionaryType>> getCIList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize,
                                                  @RequestParam(value = "content",required = false) String content) {
        LambdaQueryWrapper<DictionaryType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.apply("1=1");
        queryWrapper.eq(StringUtils.isNotEmpty(content),DictionaryType::getContent,content);

        Page<DictionaryType> page = new Page<>(pageIndex, pageSize);
        Page<DictionaryType> page1 = dictionaryTypeService.page(page, queryWrapper);


        return ResultUtil.success(page1);
    }
}